package com.hotel.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.mapper.WorkerSalariesMapper;
import com.hotel.pojo.WorkerSalaries;
import com.hotel.service.WorkerSalariesService;
import com.hotel.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author by LuJinRong
 * @date 2021/7/24。
 * @time 11:36。
 */

@Service
public class WorkerSalariesServiceImpl implements WorkerSalariesService {

    @Autowired
    private WorkerSalariesMapper workerSalariesMapper;
    @Override
    public PageResult findAll(PageResult pageResult) {
        //1.定义MP的分页对象 arg1:页面   arg2:行数
        IPage iPage = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        //2.构建查询条件构造器
//        QueryWrapper queryWrapper = new QueryWrapper();
      // List<WorkerSalaries> list = workerSalariesMapper.selectList(null);
        //判断用户数据是否有效  有效 true   无效 false
//        boolean flag = StringUtils.hasLength(pageResult.getQuery());
//        queryWrapper.like(flag, "workerName", pageResult.getQuery());
        //经过MP分页查询将所有的分页(total/结果/页面/条数/xxx)数据封装到iPage对象
        iPage = workerSalariesMapper.selectPage(iPage,null);
        //从分页对象中获取总记录数
        long total = iPage.getTotal();
        System.out.println(total);
        //从分页对象中获取分页后的结果
        List<WorkerSalaries> rows = iPage.getRecords();                                                            
        return pageResult.setTotal(total).setRows(rows);
        //需要返回的是5个参数
    }

    @Override
    //@Transactional
    public void deleteWorkerSalaries(Integer id) {
        workerSalariesMapper.deleteById(id);
    }

    @Override
    public WorkerSalaries getUserById(Integer id) {
        return workerSalariesMapper.selectById(id);
    }

    @Override
    public void addUser(WorkerSalaries workerSalaries) {
        //Integer monthlyPay = subsidy + basePay + travelAllowance - deductMoney;
        int monthlyPay1 =  workerSalaries.getSubsidy()+workerSalaries.getBasePay()+workerSalaries.getTravelAllowance()
                -workerSalaries.getDeductMoney();
        WorkerSalaries monthlyPay = workerSalaries.setMonthlyPay(monthlyPay1);

        workerSalariesMapper.insert(workerSalaries);
    }


}
